home *** CD-ROM | disk | FTP | other *** search
- Path: news.lpr.carel.fi!usenet
- From: Ari Lukumies <aril@cmt.lpr.mail.carel.fi>
- Newsgroups: comp.lang.c
- Subject: Re: Suggestions welcome
- Date: Thu, 08 Feb 1996 14:25:47 +0200
- Organization: Carelcomp Forest
- Message-ID: <3119EBCB.776A@cmt.lpr.mail.carel.fi>
- References: <1996Feb6.225454.7550@venus.gov.bc.ca>
- NNTP-Posting-Host: renoir.cclahti.carel.fi
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0b6a (WinNT; I)
-
- Steve Smith wrote:
- >
- > I am writing a C++ Console application to extract information from a
- > proprietary database file.
- >
- > In order to interpret some of the data in a record, I need to read in
- > a flat file which contains lists of values for certain variables. A
- > sample would be:
- >
- > ++$ CITYNAMES
- > VICTORIA
- > VANCOUVER
- > EDMONTON
- > CALGARY
- > TORONTO
- > MONTREAL
- > QUEBEC
- > ++$ END
- >
- > The city name field in the database record is a single byte, where 01 =
- > VICTORIA, 02 = VANCOUVER etc.
- >
- > There are a number of such lists in the flat file, several have > 200 entries.
- > I want to read these lists in to my program, and be able to access them
- > using an array index (such as CityName[i]).
- >
- > I don't know how many elements will be in the array from one run to the
- > next, so I can't (and wouldn't want to) statically allocate an array.
- >
- > The only way I can think of to accomplish this, is the read through the
- > flat file twice.
- >
- > On the first pass, count the number of elements required for each
- > unique list. Once I know how many elements are required I can dynamically
- > allocate an array of pointers to string for each list.
- >
- > On the second pass through the file, I can allocate storage for each string,
- > copy it into memory and initialize the char* array with the pointer.
- >
- > If anyone has any other suggestions on how I could accomplish this, I would be
- > interested in hearing from them (either e-mail or post).
- >
- > Thanks in advance
- >
-
- I often use constructs such as this (this is just written offhand and not tested, but
- you'll get the idea):
-
- typedef struct _things {
- struct _things *next;
- char *stuff_goes_here;
- } THINGS;
-
- THINGS *head = NULL;
- THINGS *tail = NULL;
-
- THINGS *AddThing(void)
- {
- if (!tail)
- tail = head = (THINGS *)calloc(1, sizeof(THINGS));
- else
- tail = tail->next = (THINGS *)calloc(1, sizeof(THINGS));
- if (!tail) {
- /* ERROR!! Out of memory */
- }
- return tail;
- }
-
- ...
-
- /* add things */
- THINGS *tmp = AddThing();
- tmp->stuff_goes_here = (char *)malloc(enough);
- strcpy(tmp->stuff_goes_here, the_stuff);
-
- ...
-
- /* walk the list */
- THINGS *p;
-
- for (p = head; p != NULL; p = p->next) {
- /* do something with p->stuff_goes_here */
- }
-
- ...
- /* clear the list */
-
- THINGS *p, *q;
-
- for (p = head, q = NULL; p != NULL; p = q) {
- q = p->next;
- if (p->stuff_goes_here)
- free(p->stuff_goes_here);
- free(p);
- }
-
- HTH,
- AriL
- --
- All my opinions are mine and mine alone.
-